package com.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.ssm.pojo.Items;
import com.ssm.pojo.QueryVO;
import com.ssm.service.ItemService;

@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/test.action")
	@ResponseBody
	public List<Items> findList(Model model){
	List<Items> items =	itemService.findItem();
		return items;
	}
	
	@RequestMapping("/itemList.action")
	public String showList(Model model){
	List<Items> items =	itemService.findItem();
	model.addAttribute("itemList", items);
		return "itemList";
	}
	@RequestMapping(value="/itemEdit",method={RequestMethod.GET,RequestMethod.POST})
	public String editItems(Model model,Items item){
		Items  items= itemService.findById(item);		
		model.addAttribute("item", items);
		return "editItem";
	}
	@RequestMapping("/updateitem")
	public String updateitem(Items item,MultipartFile picture) throws Exception{
		if (true) {
			throw new Exception("如果世界漆黑");
		}
		System.out.println(2/0);
//		源文件的名称
		String originalFilename = picture.getOriginalFilename();
//		新文件的名称
		String newfile = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
//		上传的图片
		File file = new File("D:/picture",newfile);
		item.setPic(newfile);
//		图片上传到目录
		picture.transferTo(file);
		itemService.save(item);
		return "redirect:itemList.action";
	}
	@RequestMapping("/queryitem")
	public String findByConfition(Model model , QueryVO queryVO,String[] ids){
			List<Items> items =	itemService.findByConfitionidOrName(queryVO);
			model.addAttribute("itemList", items);
				return "itemList";
			}
		
}
